package com.snailxr.base.task.dao.impl;

import javax.annotation.Resource;
import javax.swing.text.Document;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCollection;
import com.snailxr.base.task.dao.DataTransferDao;
@Repository("dataTransferDaoImpl")
public class DataTransferDaoImpl implements DataTransferDao {

	public final Logger log = Logger.getLogger(this.getClass());
	@Resource(name="mongoTemplate")
	private MongoTemplate mongoTemplate;
	/**
	 * 删除全部数据
	 */
	@Override
	public void delete(String collectionName) {
		mongoTemplate.findAllAndRemove(new Query(), collectionName);
	}
	/**
	 * 插入一条数据
	 */
	@Override
	public void insert(JSONObject json, String collectionName) {
		mongoTemplate.insert(json,collectionName);
	}
	/**
	 * 查询全部
	 */
	@Override
	public JSONArray selectAll(String collectionName) {
		DBCollection collection  = mongoTemplate.getCollection(collectionName);
		DBCursor cursor =  collection.find();
		String jsonArrStr = cursor.toArray().toString();
		JSONArray jsonArr = JSON.parseArray(jsonArrStr);
		return jsonArr;
		
	}
	
}
